﻿'Imports CoreFX.Interfaces, CoreFX.Logging
Namespace Client.Channel
    <Serializable()> _
    Public Class IRCChannel
        Delegate Sub Nav()
        Event CreateChatWindow()
        Event TabIsLoadedChanged()
        Friend Cname As String
        Friend CTabVar As Object
        Friend FCWVar As Client.IChannelWindow
        Friend TopicVar, ChanNameVar As String
        Friend TabIsLoadedVar As Boolean
        Friend Users As Users.UserList
        Public Sub New(ByVal ChannelName As String)
            ChanName = ChannelName
        End Sub
        Public Property UserListProperty() As Users.UserList
            Get
                Return Users
            End Get
            Set(ByVal value As Users.UserList)
                Users = value
            End Set
        End Property
        Public Property TabIsLoaded() As Boolean
            Get
                Return TabIsLoadedVar
            End Get
            Set(ByVal value As Boolean)
                TabIsLoadedVar = value
                RaiseEvent TabIsLoadedChanged()
            End Set
        End Property
        Public Property ChannelTab() As Object
            Get
                Return CTabVar
            End Get
            Set(ByVal value As Object)
                CTabVar = value
            End Set
        End Property
        Public Property ChannelWindow() As Client.IChannelWindow
            Get
                Return FCWVar
            End Get
            Set(ByVal value As Client.IChannelWindow)
                FCWVar = value
            End Set
        End Property
        Public Property ChanName() As String
            Get
                Return ChanNameVar
            End Get
            Set(ByVal value As String)
                ChanNameVar = value
            End Set
        End Property
        Public Property ChanTopic() As String
            Get
                Return TopicVar
            End Get
            Set(ByVal value As String)
                TopicVar = value
            End Set
        End Property
        Public Sub MakeChatWindow(ByVal ChatEngineVar As IRCClient)
            AddHandler ChatEngineVar.TopicChange, AddressOf ChangeTopic
            AddHandler ChatEngineVar.NewMessageRcvd, AddressOf NewMessage
            AddHandler ChatEngineVar.KickedFromChan, AddressOf Kicked
            AddHandler ChatEngineVar.UserJoined, AddressOf UserJoined
            AddHandler ChatEngineVar.RecvNickList, AddressOf SetNickList
            AddHandler ChatEngineVar.ChannelModeChanged, AddressOf CMCHandled
            AddHandler ChatEngineVar.NewQuery, AddressOf HandleAQuery
            AddHandler ChatEngineVar.UserModeChanged, AddressOf ModeChange
            AddHandler ChatEngineVar.IRCNoticedRecv, AddressOf IRCNoticeHandle
            RaiseEvent CreateChatWindow()
        End Sub
        Public Sub Dispose(ByVal ChatEngineVar As IRCClient)
            RemoveHandler ChatEngineVar.TopicChange, AddressOf ChangeTopic
            RemoveHandler ChatEngineVar.NewMessageRcvd, AddressOf NewMessage
            RemoveHandler ChatEngineVar.KickedFromChan, AddressOf Kicked
            RemoveHandler ChatEngineVar.UserModeChanged, AddressOf ModeChange
            RemoveHandler ChatEngineVar.UserJoined, AddressOf UserJoined
            RemoveHandler ChatEngineVar.RecvNickList, AddressOf SetNickList
            RemoveHandler ChatEngineVar.ChannelModeChanged, AddressOf CMCHandled
            RemoveHandler ChatEngineVar.IRCNoticedRecv, AddressOf IRCNoticeHandle
            RemoveHandler ChatEngineVar.NewQuery, AddressOf HandleAQuery
            ChanTopic = Nothing
            ChannelWindow = Nothing
            ChannelTab = Nothing
            ChatEngineVar = Nothing
        End Sub
        Private Sub ModeChange(ByVal E As ChatEngine.Connections.UserModeArgs)
            ' MsgBox("Mode on " & E.User & " changed to " & E.Mode & " " & E.ModeAction)
            If E.Channel = ChanName Then ChannelWindow.ChangeUserMode(E)
        End Sub
        Private Sub IRCNoticeHandle(ByVal E As Connections.GenericEventArgs)
            ChannelWindow.HandleNotice(E)
        End Sub
        Private Sub Disconnected()
            For Each Item As ChannelItem In SharedVars.Channels
                If Not IsNothing(Item.ChanObj.ChannelWindow) Then
                    MsgBox("Disconnected")
                End If
            Next
        End Sub
        Private Sub HandleAQuery(ByVal User As String, ByVal Message As String, ByVal IsFirstMessage As Boolean)

        End Sub
        Private Sub CMCHandled(ByVal E As Connections.ChannelModeArgs)
            'If E.ChannelName = ChanName Then ChannelWindow.ChannelModeChanged(E)
        End Sub
        Private Sub SetNickList(ByVal Channel As String, ByVal E As Connections.NickListRecvArgs)
            If Channel = ChanName And Not IsNothing(ChannelWindow) Then ChannelWindow.LoadNickList(Channel, E)
        End Sub
        Private Sub ShowFCW()
        End Sub
        Private Sub ChangeTopic(ByVal Topic As String, ByVal ChannelName As String)
            If ChannelName = ChanName Then
                ChanTopic = Topic
                ChannelWindow.ChangeTopic(Topic, ChannelName)
            End If
        End Sub
        Public Sub NewMessage(ByVal User As String, ByVal Message As String, ByVal ChannelName As String)
            If LCase(ChannelName) = LCase(ChanName) Then
                ChannelWindow.NewMessage(User, Message, ChannelName)
            End If
        End Sub
        Private Sub UserJoined(ByVal User As String, ByVal ChannelName As String)
            If ChanName = ChannelName Then ChannelWindow.UserJoined(User, ChannelName)
        End Sub
        Public Sub Kicked(ByVal E As Connections.KickedEventArgs)
            If LCase(ChanName) = LCase(E.Channel) Then ChannelWindow.Kicked(E)
        End Sub
    End Class
End Namespace